package cn.dengta.webapp.trade.vo;

import java.util.*;
import java.util.stream.Collectors;

import cn.dengta.common.model.Status;
import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.base.vo.AbstractVO;
import cn.dengta.webapp.trade.entity.WesKitConfig;
import cn.dengta.webapp.user.model.Gender;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.X;

@Setter
@Getter
public class WesKitConfigItemVO extends AbstractVO<WesKitConfig> {

	/** 当前包 */
	WesKitInfo wesKit;
	/** 男用户来源包，逗号分割 */
	List<WesKitInfo> manKits;
	/** 女用户来源包，逗号分割 */
	List<WesKitInfo> womanKits;
	/** 状态 1=开启 0=关闭 */
	Integer status;

	List<WesKit> selectManKits;

	List<WesKit> selectWomanKits;
	/** 重新注册 */
	Integer reRegister;


	public List<WesKit> getSelect(Integer likeGender) {
		if (isSelectAll(likeGender)) {
			return Arrays.stream(WesKit.values()).toList();
		}
		boolean isMan = Objects.equals(Gender.MALE.value, likeGender);
		if (isMan) {
			return getSelectManKits();
		}
		return getSelectWomanKits();
	}

	public boolean isOpen() {
		return status != 0;
	}

	public boolean isSelectAll(Integer likeGender) {
		boolean isMan = Objects.equals(Gender.MALE.value, likeGender);
		if (isMan) {
			return !X.isValid(getSelectManKits());
		}
		return !X.isValid(getSelectWomanKits());
	}

	public boolean getKitSwitch() {
		return Status.asResult(status);
	}

	public void setWesKit(WesKit wesKit) {
		this.wesKit = WesKitInfo.of(wesKit, null);
	}

	public void setManKits(List<WesKit> selects) {
		this.manKits = Arrays.stream(WesKit.values()).map(kit -> WesKitInfo.of(kit, selects.contains(kit))).collect(Collectors.toList());
	}

	public void setWomanKits(List<WesKit> selects) {
		this.womanKits = Arrays.stream(WesKit.values()).map(kit -> WesKitInfo.of(kit, selects.contains(kit))).collect(Collectors.toList());
	}

	public List<WesKit> getSelectManKits() {
		if (selectManKits == null) {
			selectManKits = manKits.stream().filter(WesKitInfo::getSelect).map(WesKitInfo::getWesKit).collect(Collectors.toList());
		}
		return selectManKits;
	}

	public List<WesKit> getSelectWomanKits() {
		if (selectWomanKits == null) {
			selectWomanKits = womanKits.stream().filter(WesKitInfo::getSelect).map(WesKitInfo::getWesKit).collect(Collectors.toList());
		}
		return selectWomanKits;
	}

	@Getter
	@Setter
	public static class WesKitInfo {
		Integer value;
		String label;
		Boolean select;
		WesKit wesKit;

		public static WesKitInfo of(WesKit wesKit, Boolean kitSwitch) {
			WesKitInfo info = new WesKitInfo();
			info.value = wesKit.value;
			info.label = wesKit.getLabel();
			info.select = kitSwitch;
			info.wesKit = wesKit;
			return info;
		}
	}

}
